package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.example.demo.po.*;
import com.example.demo.service.TBizArtUserService;
import com.example.demo.service.UserManagerService;
import com.example.demo.util.UploadALiYunOOS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tbaUser")
public class TBAUserController {

    @Autowired
    private TBizArtUserService tBAUserService;

    @RequestMapping(value = "/list")
    public ModelAndView list(@ModelAttribute TBizArtUser tBAUser, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        PageRequest page = new PageRequest();
        page.setPageNum(tBAUser.getPageNum());
        page.setPageSize(tBAUser.getPageSize());
        PageResult resilt = tBAUserService.findByPage(tBAUser,page);
        List<TBizArtUser> list = (List<TBizArtUser>) resilt.getContent();
        if(list!=null&&list.size()>0){
            UploadALiYunOOS oss = new UploadALiYunOOS();
            OSSClient ossClient =oss.creatOSSClient();
            for (TBizArtUser user:list){
                //图片转换
                if(user.getMainpic()!=null){
                    String path = oss.styleQuery(ossClient,user.getMainpic().toString(),"style/ys320");
                    user.setMainpicShow(path);
                }
            }
            ossClient.shutdown();
            resilt.setContent(list);
        }
        view.setViewName("/tbaUser/tbaUser_list");
        view.addObject("result", JSON.toJSON(resilt));
        return  view;
    }

    @RequestMapping(value = "/saveInit")
    public ModelAndView saveInit(@ModelAttribute TBizArtUser tBAUser,HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        if(tBAUser.getId()!=null&&!"".equals(tBAUser.getId())){
            TBizArtUser user =tBAUserService.findById(tBAUser.getId());
            UploadALiYunOOS oss = new UploadALiYunOOS();
            OSSClient ossClient =oss.creatOSSClient();
            String path = oss.styleQuery(ossClient,user.getMainpic().toString(),"style/ys320");
            user.setMainpicShow(path);
            view.addObject("user",user);
        }
        view.setViewName("/tbaUser/tbaUser_save");
        return  view;
    }


    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public String saveOrUpdate(@RequestBody TBizArtUser tBAUser, HttpServletRequest request, HttpSession session) {
        if(tBAUser.getId()!=null&&!"".equals(tBAUser.getId()))
            tBAUserService.update(tBAUser,request);
        else
            tBAUserService.save(tBAUser,request);
        return "success";
    }

    @RequestMapping(value = "/del")
    public String del(@RequestParam("id") Integer id){
        if(tBAUserService.del(id))
            return "success";
        else
            return "fail";
    }


}
